# -*- coding: utf-8 -*-
# 爬虫文件描述
# /*********************************************************
# Copyright @ 苏州瑞泰信息技术有限公司 All rights reserved.
# 创建人   : Luke Chen
# 创建时间 : 2023/5/23 13:09
# 说明     : 爬取ylys
# *********************************************************/
# import requests
# import re
# from lxml import etree
# request = requests.get('http://www.cnkang.com/yyk/docindex/374568/')
# response = etree.HTML(request.text)
# print(response.xpath(
#             "//div[2]/div/dl/dd/ul/text()")[0])
# a = response.xpath(
#             "//div[2]/div/dl/dd/ul/text()")[0]
# pat = r'[ ]'
# print(re.split(pat,a)[0])
# print(re.split(pat,a)[-1])
# print(request.text)
from selenium import webdriver
import time
import re
import csv
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('-headless')
# chrome_options.add_argument('--window-size=1920,1080')
# chrome_options.add_argument('lang = zh_CN.UTF - 8')
browser = webdriver.Chrome(chrome_options=chrome_options)

browser.get('https://mip.mfk.com/doctor/?area_id=1')
time.sleep(10)
browser.find_element_by_xpath('//div[3]/div[2]/dl[1]/dt').click()
time.sleep(5)
# print(browser.find_element_by_xpath('//div[4]/div/div[1]/ul/li[1]/span').text)
pro = browser.find_elements_by_xpath('//div[4]/div/div[1]/ul/li')
# print(pro)
for i in range(1, len(pro)+1):
    code = browser.find_element_by_xpath('//div[4]/div/div[1]/ul/li[{}]'.format(i)).get_attribute('onclick')
    pat = r"[(,']"
    code1 = re.split(pat, code)[1]
    name = browser.find_element_by_xpath('//div[4]/div/div[1]/ul/li[{}]/span'.format(i)).text
    if name == '营养科' or name == '整形美容科':
        code2 = ''
        name2 = ''
        csvFile = open('der_place.csv', 'a', encoding='utf-8', newline='')
        writer = csv.writer(csvFile)
        writer.writerow([code1, name, code2, name2])
    else:
        browser.find_element_by_xpath('//div[4]/div/div[1]/ul/li[{}]'.format(i)).click()
        time.sleep(5)
        # pro = browser.find_elements_by_xpath('//div[4]/div/div[2]/ul')
        # for b in range(1, len(pro) + 1):
        #     print(b)
        # pro1 = browser.find_elements_by_xpath('//div[4]/div/div[2]/ul[{}]/li'.format(b))
        pro1 = browser.find_elements_by_xpath('//div[4]/div/div[2]/ul[{}]/li'.format(i))
        for a in range(2, len(pro1) + 1):
            print(a)
            code = browser.find_element_by_xpath('//div[4]/div/div[2]/ul[{}]/li[{}]'.format(i, a)).get_attribute('onclick')
            pat = r"[(,']"
            code2 = re.split(pat, code)[1]
            name2 = browser.find_element_by_xpath('//div[4]/div/div[2]/ul[{}]/li[{}]/span'.format(i, a)).text
            # print(code1, name, code2, name2)
            # colums = ['code1', 'name', 'code2', 'name2']
            # with open('mfk_place.csv', 'a', encoding='utf-8', newline='') as file:
            #     res = csv.writer(file)
            # res.writerow(colums)
            csvFile = open('der_place.csv', 'a', encoding='utf-8', newline='')
            writer = csv.writer(csvFile)
            writer.writerow([code1, name, code2, name2])
# a = browser.find_element_by_xpath('//div[4]/div[1]/ul/li[2]').get_attribute('onclick')
# print(a)
browser.quit()